package com.thomas.framework.controller.admin;

import com.thomas.framework.core.AppException;
import com.thomas.framework.core.annotation.Function;
import com.thomas.framework.core.annotation.FunctionMenu;
import com.thomas.framework.entity.SysModel;
import com.thomas.framework.entity.SysRole;
import com.thomas.framework.entity.SysUser;
import com.thomas.framework.service.ISysModelService;
import com.thomas.framework.service.ISysRoleService;
import com.thomas.framework.utils.CommonUtil;
import com.thomas.framework.utils.JsonUtil;
import com.thomas.framework.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashSet;
import java.util.List;

/**
 * SysRole Controller
 * Created by taohj on 2014/5/5.
 */

@FunctionMenu("SysRoleManagement")
@RequestMapping("admin/sys/role/")
@Controller
public class SysRoleController {


    @Autowired
    private ISysRoleService service;


    @Autowired
    private ISysModelService modelService;




    @Function("SysRoleCreateView")
    @RequestMapping("createView")
    public  String createView(){
        return  "admin/sys/role/create";
    }


    @Function("SysRoleCreate")
    @ResponseBody
    @RequestMapping("create")
    public  String create(
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "description",required = false) String description
    ){
        PageResult pageResult=new PageResult();
        SysRole role=new SysRole();
        role.setName(name);
        role.setDescription(description);
        pageResult.setSuccess(service.create(role));
        return pageResult.toString();
    }



    @Function("SysRoleRemove")
    @ResponseBody
    @RequestMapping("remove")
    public  String remove(@RequestParam("id") String id){
        PageResult pageResult=new PageResult();
        SysRole role=new SysRole();
        role.setId(id);
        pageResult.setSuccess(service.remove(role));
        return  pageResult.toString();
    }


    @Function("SysRoleModifyView")
    @RequestMapping("modifyView")
    public  String  modifyView(HttpServletRequest request,
                               @RequestParam("id") String id){
        SysRole role=service.getById(new SysRole(id));
        request.setAttribute("role",role);
        return "admin/sys/role/modify";
    }

    @Function("SysRoleModify")
    @ResponseBody
    @RequestMapping("modify")
    public  String modify(
            @RequestParam(value = "id",required = false) String id,
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "description",required = false) String description
    ){
        PageResult pageResult=new PageResult();
        SysRole role=new SysRole();
        role.setId(id);
        role.setName(name);
        role.setDescription(description);
        pageResult.setSuccess(service.modify(role));
        return  pageResult.toString();
    }




    @Function("SysRoleManagement")
    @RequestMapping("management")
    public  String listView(){

        return  "admin/sys/role/management";
    }



    @Function("SysRoleList")
    @ResponseBody
    @RequestMapping("list")
    public String list(
             HttpServletRequest request,
            @RequestParam(value="id",required = false) String id,
             @RequestParam(value="name",required = false) String name
    ) {
        PageResult<SysRole> pageResult = new PageResult<SysRole>();
        pageResult.setT(new SysRole());
        pageResult.getT().setId(id);
        pageResult.getT().setName(name);

        CommonUtil.initPageResult(pageResult,request);

        pageResult=service.getByPage(pageResult);
        return JsonUtil.toString(pageResult);
    }




    //授权页面
    @Function("SysRoleAuthorizeView")
    @RequestMapping("authorizeView")
    public  String  authorizeView(
            HttpServletRequest request,
            @RequestParam("id") String id
    ){
        SysRole role=service.getById(new SysRole(id));
        request.setAttribute("role",role);
        return "admin/sys/role/authorize";
    }


    //授权数据列表
    @Function("SysRoleAuthorizeList")
    @RequestMapping("authorizeList")
    @ResponseBody
    public  String  authorizeList(
            @RequestParam("id") String id
    ){
        SysRole role=service.getById(new SysRole(id));
        List<SysModel> list=modelService.getTreeModel(role);

        return  JsonUtil.toString(list);
    }



    //授权
    @Function("SysRoleAuthorize")
    @RequestMapping("authorize")
    @ResponseBody
    public  String  authorize(
            HttpSession session,
            @RequestParam("id") String id,
            @RequestParam(value = "mid",required = false) String [] mids
    ){

        SysUser user= (SysUser) session.getAttribute("user");
        if (user==null){
            throw new AppException("not login~~");
        }

        SysRole role=service.getById(new SysRole(id));
        role.setModels(new HashSet<SysModel>());
        for (int i = 0; mids!=null && i <mids.length ; i++) {
             role.getModels().add(new SysModel(mids[i]));
        }


        PageResult<SysRole> pageResult=new PageResult<SysRole>();
        pageResult.setSuccess(service.authorize(user,role));



        return pageResult.toString();
    }


}
